//
//  GoOutAcePackDetailViewController.m
//  ArtOfLiving
//
//  Created by 刘虎 on 16/5/3.
//  Copyright © 2016年 刘虎. All rights reserved.
//

#import "GoOutAcePackDetailViewController.h"
#import "GoOutAcePackDetailModel.h"

@interface GoOutAcePackDetailViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSMutableDictionary *parameters;                      //请求参数
@property (nonatomic, strong) GoOutAcePackDetailModel *model;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation GoOutAcePackDetailViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.navigationItem.title = self.navCTitle;
    [self.view addSubview:self.tableView];
    [self fetchDataSourceWithUrlString:kAcePackTopicDetailInfo parameter:self.parameters];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - -----tableView的代理方法-----
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        
        return (self.model.cover.length == 0) ? 0 : 1;
    } if (section == 1) {
        
        return (self.model.authorsModel.intro.length == 0) ? 0 : 1;
    } else {
        
        return self.model.related_guidesMArr.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    BaseCell *cell = nil;
    if (indexPath.section == 0) {
        
        cell = [tableView dequeueReusableCellWithIdentifier:@"GoOutAcePackDetailCoverCELL" forIndexPath:indexPath];
        [cell setCellWithModel:self.model];
    } else if (indexPath.section == 1) {
        
        cell = [tableView dequeueReusableCellWithIdentifier:@"GoOutAcePackAuthorsInfoCELL" forIndexPath:indexPath];
        [cell setCellWithModel:self.model.authorsModel];
    } else {
        
        cell = [tableView dequeueReusableCellWithIdentifier:@"GoOutAcePackDetailCELL" forIndexPath:indexPath];
        [cell setCellWithModel:self.model.related_guidesMArr[indexPath.row]];
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        CGSize size = [self.model.info boundingRectWithSize:CGSizeMake(kScreenWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil].size;
        return kScreenWidth*(420.0/670) + size.height + 40;
    } else if (indexPath.section == 1) {
        
        CGSize size = [self.model.authorsModel.intro boundingRectWithSize:CGSizeMake(kScreenWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil].size;
        return size.height + 95.0f;
    } else {
        
        return 100.0f;
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    if (section == 2) {
        
        return @"相关锦囊";
    } else {
        
        return 0;
    }
}

#pragma mark - ----数据处理-----
- (void)fetchDataSourceWithUrlString:(NSString *)urlString parameter:(NSMutableDictionary *)parameter {
    
    [LHDataRequestTools getFetchDataWithUrlString:urlString paramenters:parameter success:^(id data) {
        
        NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        [self.model setValuesForKeysWithDictionary:resultDic[@"data"]];
        [self.tableView reloadData];
    } fail:^{
        
        NSLog(@"锦囊详情界面数据请求出错");
    } view:self.view];
}


#pragma mark - ----属性懒加载-----
- (NSMutableDictionary *)parameters {
    
    if (!_parameters) {
        
        _parameters = [@{@"client_id":@"qyer_android", @"client_secret":@"9fcaae8aefc4f9ac4915", @"v":@1, @"track_deviceid":@863151029008023, @"track_app_version":@"6.8.1", @"track_app_channel":@"qq", @"track_device_info":@"mx3", @"track_os":@"Android4.4.4", @"app_installtime":@1446538281848, @"lat":@40.038026, @"lon":@116.370336, @"id":self.guideID} mutableCopy];
    }
    return _parameters;
}

- (GoOutAcePackDetailModel *)model {
    
    if (!_model) {
        
        _model = [[GoOutAcePackDetailModel alloc] init];
    }
    return _model;
}

- (UITableView *)tableView {
    
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-64) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerNib:[UINib nibWithNibName:@"GoOutAcePackDetailCell" bundle:nil] forCellReuseIdentifier:@"GoOutAcePackDetailCELL"];
        [_tableView registerNib:[UINib nibWithNibName:@"GoOutAcePackDetailCoverCell" bundle:nil] forCellReuseIdentifier:@"GoOutAcePackDetailCoverCELL"];
        [_tableView registerNib:[UINib nibWithNibName:@"GoOutAcePackAuthorsInfoCell" bundle:nil] forCellReuseIdentifier:@"GoOutAcePackAuthorsInfoCELL"];
    }
    return _tableView;
}

@end
